#pragma once
#include <mutex>
#include "util.hpp"
#define CLOUD_CONFIG "./cloud_config.conf"

namespace cloud
{
  class Config
  {
  private:
    Config()
    {
      ReadConfig();
    }
    static Config* _instance;
    static std::mutex _mutex;
  
  private:
    time_t _hot_time; //热点判断时间
    int _server_port; //服务器访问端口
    std::string _server_ip; //服务器访问ip
    std::string _url_prefix; //文件下载前缀路径
    std::string _arc_suffix; //压缩包后缀名称
    std::string _pack_dir; //压缩包存放路径
    std::string _back_dir; //上传文件存放路径
    std::string _manager_file; //服务端备份信息存放文件
  
  public:
    bool ReadConfig()
    {
      FileUtil fu(CLOUD_CONFIG);
      std::string body;
      if(fu.GetFileContent(body) == false)
      {
        std::cout << "ReadConfig() error" << std::endl;
        return false;
      }
      Json::Value val;
      if(JsonUtil::UnSerialize(body, val) == false)
      {
        std::cout << "ReadConfig() error" << std::endl;
        return false;
      }
      _hot_time = val["hot_time"].asInt();
      _server_port = val["server_port"].asInt();
      _server_ip = val["server_ip"].asString();
      _url_prefix = val["url_prefix"].asString();
      _arc_suffix = val["arc_suffix"].asString();
      _pack_dir = val["pack_dir"].asString();
      _back_dir = val["back_dir"].asString();
      _manager_file = val["manager_file"].asString();
      return true;
    }
    int GetHotTime()
    {
      return _hot_time;
    }
    int GetServerPort()
    {
      return _server_port;
    }
    std::string GetServerIp()
    {
      return _server_ip;
    }
    std::string GetURLPrefix()
    {
      return _url_prefix;
    }
    std::string GetArcSuffix()
    {
      return _arc_suffix;
    }
    std::string GetPackDir()
    {
      return _pack_dir;
    }
    std::string GetBackDir()
    {
      return _back_dir;
    }
    std::string GetManagerFile()
    {
      return _manager_file;
    }

  public:
    static Config* _get_instance()
    {
      if(_instance == nullptr)
      {
        _mutex.lock();
        if(_instance == nullptr)
        {
          _instance = new Config();
        }
        _mutex.unlock();
      }
      return _instance;
    }
  };
  cloud::Config* Config::_instance = nullptr;
  std::mutex Config::_mutex; 
}
